<?php

declare (strict_types=1);
namespace App\Model;

use Hyperf\DbConnection\Model\Model;
/**
 * @property int $id 
 * @property int $from_user_id 
 * @property int $to_user_id 
 * @property string $msg_type 
 * @property string $content 
 * @property string $result_detail 
 * @property int $risk_level 
 * @property string $rongcloud_results 
 * @property string $msg_uid 
 * @property \Carbon\Carbon $created_at 
 * @property \Carbon\Carbon $updated_at 
 */
class RongcloudResult extends Model
{

    const RCTYPE = [
        'RC:HQVCMsg' => '语音',
        'JT:USER_IMAGE' => '图片',
        'RC:TxtMsg' => '文本'
    ];

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'rongcloud_results';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['from_user_id','to_user_id','msg_type','content','risk_level','risk_description','result_detail','msg_uid','created_at','updated_at'];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = ['id' => 'integer', 'from_user_id' => 'integer', 'to_user_id' => 'integer', 'risk_level' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime'];


    /**
     * 新增记录
     * @param $from_user_id
     * @param $to_user_id
     * @param $msg_type
     * @param $content
     * @param string $result_detail
     * @param string $msg_uid
     */
    public static function addRecord(int $from_user_id,int $to_user_id,$msg_type = '',$content ='',$result_detail='',$msg_uid='')
    {
           return self::create([
                'from_user_id' => $from_user_id,
                'to_user_id' => $to_user_id,
                'msg_type' => $msg_type,
                'content' => $content,
                'risk_level' => $result_detail['riskLevel'],
                'risk_description' => $result_detail['riskDescription'],
                'result_detail' => json_encode($result_detail),
                'msg_uid' => $msg_uid,
           ]);
    }

    /**
     * 删除指定内容
     * @param string $time
     */
    public static function delRecord($time = '')
    {
        $time = !empty($time) ? $time : date('Y-m-d H:i:s',strtotime('-7 day'));
        self::where('created_at','<',$time)->delete();
        log_info(__CLASS__ . "==WhereTime==" . $time);
    }

}